'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
  //获取
  async gethome() {
    const { ctx } = this;
    const data = await this.app.mysql.select('home');
    data.forEach((item, index) => {
      item.key = index + 1;
    })
    ctx.body = {
      code: 200,
      data,
    };
  }

  //搜索
  async searchhome() {
    const { ctx } = this;
    const { province, city, county, word } = ctx.query;
    let newprovince = "", newcity = "", newword = "", newcounty = "";
    if (city) newcity = city;
    if (province) newprovince = province;
    if (county) newcounty = county;
    if (word) newword = word;
    const data = await this.app.mysql.query(`SELECT * FROM home WHERE province Like "%${newprovince}%" 
    and city Like "%${newcity}%"
    and county Like "%${newcounty}%"
    and word Like "%${newword}%"`);
    data.forEach((item, index) => {
      item.key = index + 1;
    })
    ctx.body = {
      code: 200,
      data,
    };
  }

  //删除
  async deletehome() {
    const { ctx } = this;
    const { id } = ctx.query;
    const data = await this.app.mysql.delete('home', {
      id: id,
    });
    if (data.affectedRows !== 1) {
      ctx.body = {
        code: 500,
        message: "删除失败"
      };
    }
    ctx.body = {
      code: 200,
      message: "删除成功"
    };
  }

  //新增
  async addhome() {
    const { ctx } = this;
    const { arr } = ctx.request.body;
    const data = await this.app.mysql.insert('home', {
      name: arr.name,
      site: arr.province + "省" + arr.city + arr.county + arr.word,
      province: arr.province,
      city: arr.city,
      county: arr.county,
      word: arr.word,
      price: arr.price,
      rent: arr.rent,
      date: arr.newdate,
      state: 1,
      broker: "",
      home: arr.home,
      key: 0,
    });
    if (data.affectedRows !== 1) {
      ctx.body = {
        code: 500,
        message: "新增失败"
      };
    }
    ctx.body = {
      code: 200,
      message: "新增成功"
    };
  }


  //编辑
  async editorhome() {
    const { ctx } = this;
    const { arr } = ctx.request.body;
    const list = await this.app.mysql.get('home', { id: arr.id });
    const data = await this.app.mysql.update('home', {
      name: arr.name,
      site: arr.province + "省" + arr.city + arr.county + arr.word,
      province: arr.province,
      city: arr.city,
      county: arr.county,
      word: arr.word,
      price: arr.price,
      rent: arr.rent,
      date: arr.newdate,
      state: list.state,
      broker: arr.broker,
      key: list.key,
      home: arr.home,
      id: list.id
    });
    if (data.affectedRows !== 1) {
      ctx.body = {
        code: 500,
        message: "修改失败"
      };
    }
    const homelist = await this.app.mysql.select('home');
    let newhome = 0, twohome = 0, renthome = 0;
    homelist.forEach(item => {
      if (item.broker === list.broker) {
        switch (item.home) {
          case "新房":
            newhome++;
            break;
          case "二手房":
            twohome++;
            break;
          case "租房":
            renthome++;
            break;
        }
      }
    });
    let broker = await this.app.mysql.get('broker', { name: list.broker });
    if (!broker) {
      ctx.body = {
        code: 500,
        message: "指派经纪人失败"
      };
      return
    }
    await this.app.mysql.update('broker', {
      name: broker.name,
      key: broker.key,
      phone: broker.phone,
      company: broker.company,
      newhome: newhome,
      twohome: twohome,
      renthome: renthome,
      state: broker.state,
      id: broker.id
    });
    newhome = 0, twohome = 0, renthome = 0;
    homelist.forEach(item => {
      if (item.broker === arr.broker) {
        switch (item.home) {
          case "新房":
            newhome++;
            break;
          case "二手房":
            twohome++;
            break;
          case "租房":
            renthome++;
            break;
        }
      }
    });
    broker = await this.app.mysql.get('broker', { name: arr.broker });
    if (!broker) {
      ctx.body = {
        code: 500,
        message: "指派经纪人失败"
      };
      return
    }
    await this.app.mysql.update('broker', {
      name: broker.name,
      key: broker.key,
      phone: broker.phone,
      company: broker.company,
      newhome: newhome,
      twohome: twohome,
      renthome: renthome,
      state: broker.state,
      id: broker.id
    });
    ctx.body = {
      code: 200,
      message: "修改成功"
    };
  }

  //开关的编辑
  async statehome() {
    const { ctx } = this;
    const { id } = ctx.request.body;
    let list = await this.app.mysql.get('home', { id: id });
    list.state = !list.state;
    const data = await this.app.mysql.update('home', {
      ...list
    });
    if (data.affectedRows !== 1) {
      ctx.body = {
        code: 500,
        message: "修改失败"
      };
    }
    ctx.body = {
      code: 200,
      message: "修改成功"
    };
  }
}

module.exports = HomeController;
